
# coding: utf-8

# In[1]:

import requests 
import time
from bs4 import BeautifulSoup
import lxml
import json
from Crypto.Cipher import AES
import base64
import os
import binascii
from builtins import pow
from builtins import int
from builtins import map
from builtins import chr


# In[2]:

header = {'Host': 'music.163.com',
          'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
          'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
          'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
          'Accept-Encoding': 'gzip, deflate',
          'Connection': 'keep-alive',
          'Upgrade-Insecure-Requests': '1',
          'Cache-Control': 'max-age=0',
          'cookie':'appver=1.5.2' 
         }


# In[3]:

# with requests.Session() as s:
# #     s = requests.Session()
#     param = {'s':'汪峰', 'type':'1'}
#     req_begin_1 = s.get('http://music.163.com/search', headers=header)
#     req_1 = s.get('http://music.163.com/search/', data=param, headers=header)
#     req_2 = s.get('http://music.163.com/search', data=param, headers=header)
#     print(req_2.request.headers)
    


# In[4]:

dic = {"hlpretag":"<span class='s-fc7'>","hlposttag":"</span>","limit":"30","offset":"0","s":"lizhi","total":"true","type":"1","csrf_token":""}
# j8b_first_param = json.dumps(dic)
j8b_second_param = "010001"
j8b_third_param = '''00e0b509f6259df8642dbc35662901477df22677ec15
                    2b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2
                    e417629ec4ee341f56135fccf695280104e0312ecbda9
                    2557c93870114af6c9d05c4f7f0c3685b7a46bee25593
                    2575cce10b424d813cfe4875d3e82047b97ddef52741d
                    546b8e289dc6935b3ece0462db0a22b8e7'''
j8b_forth_param = "0CoJUm6Qyw8W8jud"
iv = "0102030405060708"
# j8b_first_param
# type(dic)


# In[5]:

j8b_first_param = '{"hlpretag":"<span class=\"s-fc7\">","hlposttag":"</span>","s":"宋冬野","type":"1","offset":"0","total":"true","limit":"30","csrf_token":""}'
# j8b_second_param = "010001"
# j8b_third_param = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"
# j8b_forth_param = "0CoJUm6Qyw8W8jud"
# iv = "0102030405060708"

def AES_encrypt(text, key, iv):
    text = text.encode('utf-8')
    pad = 16 - len(text) % 16
    text = text + (b'\n'*pad)
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    encrypt_text = encryptor.encrypt(text)
#     encrypt_text = b2a_hex(encrypt_text)
    encrypt_text_new = base64.b64encode(encrypt_text).decode()
    return encrypt_text_new

def createSecKey():
    return binascii.hexlify(os.urandom(16))[:16]

def get_params(first_key,second_key):
    iv = "0102030405060708"
#     first_key = j8b_forth_param
#     second_key = 16* 'F'
    h_encText_one = AES_encrypt(j8b_first_param, first_key, iv)
    h_encText_two = AES_encrypt(h_encText_one,second_key, iv)
    return h_encText_two

def get_encSecKey(text, pubkey, moduls):
    text = text[::-1]
    rs = pow(int(binascii.hexlify(text), 16), int(pubkey, 16), int(moduls, 16))
    return format(rs, 'x').zfill(256)

random = createSecKey()
encText = get_params(j8b_forth_param, random)
encSecKey = get_encSecKey(random, j8b_second_param, j8b_third_param)


# In[54]:

# text = j8b_first_param
# key = j8b_forth_param
# iv = "0102030405060708"
# text = text.encode('utf-8')
# # pad = 16 - len(text) % 16
# # text = text + pad * chr(pad)
# pad = 16 - (len(text) % 16)
# text = text + (b'\n'*pad)
# encryptor = AES.new(key, AES.MODE_CBC, iv)
# encrypt_text = encryptor.encrypt(text)
# base64.b64encode(encrypt_text).decode()


# In[55]:

# def get_encSecKey(text, pubkey, moduls):
#     text = text[::-1]
#     rs = pow(int(binascii.hexlify(text), 16), int(pubkey, 16), int(moduls, 16))
#     return format(rs, 'x').zfill(256)
# #     return encSecKey

# def createSecKey():
#     return binascii.hexlify(os.urandom(16))[:16]

# # # 作者：平胸小仙女
# # # 链接：https://www.zhihu.com/question/36081767/answer/140287795
# # # 来源：知乎
# # # 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
    
# SecKey = createSecKey()

# # encSecKey_1 = "9e92eff1e7b3881cce33cce4db62f0f95067b0ecfc2e26ec04294d3c800ad0960803f20a7dd0e09e2dabce0e2baf75a8669e426ea4447a2aadcb110ce5ddea7bf47112487fe577416edb3f97c3c57cfbba14f1791c8f01e12de367a3f2c9b3591031014e1adc07baf9a4488bc2312f7977d596fad2d5fd81f79f58c90bfe15ed"
# encSecKey


# In[132]:

# def get_json(params, encSecKey):
#     url = 'http://music.163.com/weapi/cloudsearch/get/web?csrf_token='
#     data = {'param': params,'encSecKey': encSecKey}
#     with requests.Session() as s:
#         response = s.post(url, data=data,headers=header)
#     return response.json()


# In[68]:

# # if __name__ == '__main__':
# params = get_params()
# encSecKey = get_encSecKey()
# json_text = get_json(params, encSecKey)
# # json_dict = json.loads(json_text)
# print(json_text)
# # params, encSecKey
data = {
    'params':"PLL+E4bMunh8HciAzYTuC1Q8/8HiaADmAVUkN5WNMM1Vn75ggfw+OBby/jFwIoAp7AIGEq+OwTfyvDeHsNJry072h6Uz+jeZOJIfRZi7njKxD2J5LEqCkMpDkMxbtlq7bTRhWJzI5qJjwGXxbSiSnbbpN0n2J16RhGMURaeOm4DnPBqWpVJ83Y6UqjusUNQMAfOEgx8ffvTZr+PdkAk6kcZMCUHS8ItSU+YtffqH3wOwoC6WmYY3b6a7x2MLpbdNUE2eYGram9arw1RAV6IY+g==",
    'encSecKey': "aed982be3200b1ea61c997fa9eac30b9d3e012bf7be2f0211eada96921cf3ba494eee29c4c65e9eab36506cabb0ecd5d6b28010644607a5c95cad67e2b7e408c04a8d1a497d10be6793b80195dd523eb0d22da68bfcafc51b8f2d0c455ede22192e5a90d796316644e266d251cf46cd41c1a3bf368498a5d014a087cd8d57cf9"
    }
len(data['params'])


# In[64]:

url = 'http://music.163.com/weapi/cloudsearch/get/web?csrf_token='
response = requests.post(url, headers=header, data=data)
print(response.text)


# In[51]:

data


# In[134]:

data.json()['result']['songs'][0]['id']


# In[117]:

os.urandom(16)


# In[32]:

ord('d')


# In[ ]:



